/*有水果类 其成员：水果名、价格
1.能通过键盘输入水果的信息，并将水果对象存入vector中
2.能对水果按照价格降序
3.定义水果篮的类，封装容器到类中，提供水果的添加，删除，排序的接口，定义为单列类*/
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
#include <memory>

class fruit
{
private:
public:
    string name;
    float price;
    fruit(string name,float price)
    :name(name),price(price){}
    ~fruit(){}
};

bool cmp(fruit a,fruit b)
{
    return a.price > b.price;
}

class basket
{
private:
    basket(){}
    static basket* instance;
    vector<fruit> abc;
public:
    static basket* getInstance()
	{
		if(instance == 0)
        {
            instance = new basket();
        }
		return instance;
	}
    // ~basket()
    // {
        // if(instance != 0)
        // {
        //     delete(instance);
        // }
    // }
    //add
    void addF()
    {
        string s1;
        float p1;
        cout<<"请输入水果名：";
        cin>>s1;
        cout<<"请输入其价格：";
        cin>>p1;
        abc.push_back(fruit(s1,p1));
        cout<<"添加成功！"<<endl;
    }
    //del
    void delF()
    {
        string s1;
        int flag=0;
        cout<<"请输入要删除的水果名：";
        cin>>s1;
        for(int i=0;i<abc.size();i++)
        {//有重复的水果 会误删
            if(s1 == abc[i].name)
            {
                abc.erase(abc.begin()+i);
                flag = 1;
            }
        }
        if(flag == 0)
        {
            cout<<"没找到相应的水果"<<endl;
        }
        else
        {
            cout<<"删除成功!"<<endl;
        }
    }
    //sort
    void sortF()
    {
        sort(abc.begin(),abc.end(),cmp);
        cout<<"排序成功"<<endl;
    }
    //display
    void display()
    {
        for(int i=0;i<abc.size();i++)
        {
            cout<<"水果名："<<abc[i].name<<"价格："<<abc[i].price<<endl;
        }
    }
};
//定义
basket* basket::instance = 0;

int main(int argc,char*argv[])
{
    basket *k1 = basket::getInstance();
    basket *k2 = basket::getInstance();
    k1->addF();
    k2->addF();
    k1->addF();
    k2->addF();
    
    k1->delF();
    k2->sortF();
    k1->display();

    auto_ptr<basket> p (k1);
    //auto_ptr<basket> p (k2);
    
    return 0;
}